package com.example.demo.controller;

import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Status;
import com.example.demo.entity.AttributeEntity;
import com.example.demo.service.IDsEntityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 相关Controller
 *
 * @author MyName
 * @version 1.0
 * @date 2020-09-03
 * Copyright © MyCompany
 */
@RestController
@RequestMapping("/colu")
@Slf4j
public class ColumnController {

    @Autowired
    IDsEntityService service;

    /***
     * 查询ViewObject的分页数据
     * <p>
     * url请求参数示例: /list?field=abc&pageIndex=1&orderBy=abc:DESC
     * </p>
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public JsonResult getViewObjectListMapping(AttributeEntity queryDto) throws Exception {
//        return super.getViewObjectList(queryDto, pagination, AttributeEntity.class);
        return JsonResult.OK(service.list());
    }

    /***
     * 根据资源id查询ViewObject
     * @param id ID
     * @return
     * @throws Exception
     */
//    @GetMapping("/{id}")
//    public JsonResult getViewObjectMapping(@PathVariable("id")Integer id) throws Exception{
//        return super.getViewObject(id, MetropolisesDetailVO.class);
//    }

    /***
     * 创建资源对象
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @PostMapping("/")
    public JsonResult createEntityMapping(@Valid @RequestBody AttributeEntity entity) throws Exception {
        try {
            service.save(entity);
        } catch (Exception e) {
            log.warn("创建操作未成功，entity=" + entity.getClass().getSimpleName());
            return new JsonResult(Status.FAIL_OPERATION);
        }
        return JsonResult.OK(Status.OK);
    }

    /***
     * 根据ID更新资源对象
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
//    @PutMapping("/{id}")
//    public JsonResult updateEntityMapping(@PathVariable("id")Integer id, @Valid @RequestBody AttributeEntity entity) throws Exception {
//        return super.updateEntity(id, entity);
//    }

    /***
     * 根据id删除资源对象
     * @param id
     * @return
     * @throws Exception
     */
//    @DeleteMapping("/{id}")
//    public JsonResult deleteEntityMapping(@PathVariable("id")Integer id) throws Exception {
//        return super.deleteEntity(id);
//    }

} 